<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>Promise</title>
</head>
<body>
<script>
class PromiseMy {
	constructor(callback) {
		this.resolveQueue = [];
		this.rejectQueue = [];
		this.status = 'pedding';

		const resolve = (value) => {
			this.value = value;
			if(this.status == 'pedding') {
				this.pedding = 'resolve';
				this.resolveQueue.forEach((fn) => {
					fn(value);
				});
			}
		};
		const reject = (error) => {
			this.error = error;
			if(this.status == 'pedding') {
				this.status = 'reject';
				this.rejectQueue.forEach((fn) => {
					fn(error);
				});
			}
		};
		try {
			callback(resolve, reject);
		}catch(err) {
			reject(err);
		}
	}
	then(resolve, reject) {
		if(resolve) {
			this.resolveQueue.push(resolve);
		}
		if(reject) {
			this.rejectQueue.push(reject);
		}

		return this;
	}
}
const promise1 = new PromiseMy((resolve, reject) => {
	setTimeout(() => {
		resolve(11);
	});
});
promise1.then((value) => {
	console.log(value);
}, (err) => {

}).then((value) => {
	console.log(value);
}, (err) => {

});
console.log(promise1);
</script>
</body>
</html>